package sample.qiplat.plugin.flog;

import com.rapid.api.Platform;
import com.rapid.api.ext.framework.BaseObject;
import com.rapid.api.framework.common.permission.PermissionService;
import com.rapid.api.framework.project.primary.ProjectLifecycleEvent;
import com.rapid.api.logging.Diagnostic;
import com.rapid.api.logging.LogCategory;
import com.rapid.api.logging.LogMessage;
import com.rapid.api.logging.Logger;
import com.rapid.api.logging.PrintListener;
import com.rapid.api.project.Project;
import com.rapid.api.project.ProjectManager;
import com.rapid.framework.extension.annotation.Import;

public class MyProjectLifecycleEvent extends BaseObject implements ProjectLifecycleEvent {
    @Import
    private PermissionService permissionProvider;
    private Logger logger;
    private FloatWindowManager floatWindowManager;

    public MyProjectLifecycleEvent() {
        this.logger = Platform.getLogger();
        this.floatWindowManager = FloatWindowManager.getInstance();
    }

    @Override
    public void beforeLoadingProject(Project project, ProjectManager<?> projectManager) {

    }

    @Override
    public void afterLoadingProject(Project project, ProjectManager<?> projectManager) {
        if (!permissionProvider.hasSystemAlertWindowPermission()) {
            runUI(() -> {
                permissionProvider.requestSystemAlertWindowPermission((allGranted, deniedList) -> {
                    initLogger();
                });
            });
        } else {
            initLogger();
        }
    }

    @Override
    public void onCloseProject(Project project, ProjectManager<?> projectManager) {

    }

    private void initLogger() {
        runUI(() -> {
            floatWindowManager.initManager(getActivity()).showFloatWindow();
            logger.info(LogCategory.APP, "悬浮窗日志载入成功！");
        });
        logger.setPrintListener(new PrintListener() {
            @Override
            public void onPrintMessage(LogCategory logCategory, LogMessage logMessage) {
                runUI(() -> {
                    floatWindowManager.sendLog(logCategory, logMessage);
                });
            }

            @Override
            public void onPrintDiagnostic(LogCategory logCategory, Diagnostic<?> diagnostic) {
                runUI(() -> {
                    floatWindowManager.sendLog(logCategory, diagnostic);
                });
            }

            @Override
            public void onPrintTrace(LogCategory logCategory, Throwable throwable) {
                runUI(() -> {
                    floatWindowManager.sendLog(logCategory, throwable);
                });
            }

            @Override
            public void onClear(LogCategory logCategory) {
                runUI(() -> {
                    floatWindowManager.clear(logCategory);
                });
            }

            @Override
            public void onClearAll() {
                runUI(() -> {
                    floatWindowManager.clearAll();
                });
            }
        });
    }
}
